package ro.fortech.peaa.repository.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import ro.fortech.peaa.domain.Department;
import ro.fortech.peaa.repository.api.IDepartmentMapper;

/**
 * JDBC Implementation for the DepartmentMapper.
 * 
 * @author andrei
 *
 */
public class JdbcDepartmentMapper extends SimpleJdbcDaoSupport  implements IDepartmentMapper {

	/* (non-Javadoc)
	 * @see ro.fortech.peaa.repository.api.IDepartmentMapper#delete(ro.fortech.peaa.domain.Department)
	 */
	@Override
	public void delete(Department Department) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see ro.fortech.peaa.repository.api.IDepartmentMapper#find(java.lang.Integer)
	 */
	@Override
	public Department find(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see ro.fortech.peaa.repository.api.IDepartmentMapper#findAll()
	 */
	@Override
	public List<Department> findAll() {
		logger.info("START");
		List<Department> departments = getSimpleJdbcTemplate().query("SELECT " 
				+ IDepartmentMapper.ID + ", "
				+ IDepartmentMapper.NAME + ", "
				+ IDepartmentMapper.MANAGER_ID + ", "
				+ IDepartmentMapper.LOCATION_ID
				+ " FROM " + IDepartmentMapper.TABLE, new DepartmentAttrMapper()
		);
		logger.info("END - returning " + departments.size() + " results");
		return departments;
	}

	/* (non-Javadoc)
	 * @see ro.fortech.peaa.repository.api.IDepartmentMapper#insert(ro.fortech.peaa.domain.Department)
	 */
	@Override
	public void insert(Department Department) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see ro.fortech.peaa.repository.api.IDepartmentMapper#update(ro.fortech.peaa.domain.Department)
	 */
	@Override
	public void update(Department department) {
		// TODO Auto-generated method stub

	}

	private static class DepartmentAttrMapper implements ParameterizedRowMapper<Department> {
		public Department mapRow(ResultSet rs, int arg1) throws SQLException {
			Department result = new Department();
			
			result.setId(rs.getInt(IDepartmentMapper.ID));
			result.setName(rs.getString(IDepartmentMapper.NAME));
			result.setManagerId(rs.getInt(IDepartmentMapper.MANAGER_ID));
			result.setLocationId(rs.getInt(IDepartmentMapper.LOCATION_ID));
			
			return result;
		}
	}
}
